package cn.baiweigang.uu.db.mapper;

import cn.baiweigang.uu.db.dataobj.StockInfoDo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HistoryDayInfoMapper {

    @Select("select * from stock_history_day_info where dm=#{dm} order by ds desc ")
    List<StockInfoDo> queryByDm(@Param("dm") String dm);


    @Select("select * from stock_history_day_info where dm=#{dm} order by ds desc limit 1")
    StockInfoDo queryNewestByDm(@Param("dm") String dm);

    @Select("select * from stock_history_day_info where ds=#{ds} order by pm ")
    List<StockInfoDo> queryByDs(@Param("ds") String ds);

    @Select("select * from stock_history_day_info where dm=#{dm} and ds <= #{ds} order by ds desc limit #{limit}")
    List<StockInfoDo> queryByDmBeforeDS(@Param("dm") String dm, @Param("ds") String ds, @Param("limit") int limit);

    @Insert({"<script>",
            "insert into stock_history_day_info(dm,mc,updatetime,ds,o,h,l,c,v,e,zf,hs,zd,zde) values ",
            "(#{dm},#{mc},NOW(),#{ds},#{o},#{h},#{l},#{c},#{v},#{e},#{zf},#{hs},#{zd},#{zde})",
            "ON DUPLICATE KEY UPDATE",
            "<trim prefix=\"\" suffixOverrides=\"\">",
            "updatetime =NOW()",
            "</trim>",
            "</script>"
    })
    void insertOrUpdate(StockInfoDo stockInfoDo);

    @Insert({"<script>",
            "insert into stock_history_day_info(dm,mc,updatetime,ds,o,h,l,c,v,e,zf,hs,zd,zde) values",
            "<foreach collection='stockInfoDoList' item='stockInfoDo' index='index' separator=','>",
            "(#{stockInfoDo.dm},#{stockInfoDo.mc},NOW(),#{stockInfoDo.ds},#{stockInfoDo.o},#{stockInfoDo.h},#{stockInfoDo.l},#{stockInfoDo.c},#{stockInfoDo.v},#{stockInfoDo.e},#{stockInfoDo.zf},#{stockInfoDo.hs},#{stockInfoDo.zd},#{stockInfoDo.zde})",
            "</foreach>",
            "ON DUPLICATE KEY UPDATE",
            "<trim prefix=\"\" suffixOverrides=\"\">",
            "updatetime =NOW()",
            "</trim>",
            "</script>"

    })
    void insertOrUpdateBatch(@Param(value = "stockInfoDoList") List<StockInfoDo> stockInfoDoList);


    @Update("update stock_history_day_info set updatetime = NOW(), ext = #{ext} where id = #{id} ")
    void updateExt(@Param("id") String id, @Param("ext") String ext);


    @Select("select * from stock_history_day_info where dm=#{dm} and ds=#{ds}")
    StockInfoDo queryByDmDs(@Param("dm") String dm, @Param("ds") String ds);

    @Update("update stock_history_day_info set updatetime = NOW(), pm = #{pm} where id = #{id} ")
    void updateRank(@Param("id") String id, @Param("pm") String pm);
}
